import { Axios, AxiosResponse } from 'axios'
import axios from './axios'
import { assign, cloneDeep, omit } from 'lodash-es'
// get默认请求
export const get = (apiUrl: string, params?: any): Promise<AxiosResponse> => {
  console.log(params)
  let paramsData = {}
  if (params && params.pageNo && params.pageSize) {
    const data = cloneDeep(params)

    const pager = {
      pageNo: data.pageNo,
      pageSize: data.pageSize
    }
    const pagerParams = { page: JSON.stringify(pager) }
    const remainderParams = omit(data, ['pageNo', 'pageSize', 'options'])
    paramsData = assign(pagerParams, data.options, remainderParams)
  }
  return axios.get(apiUrl, { params: paramsData })
}

export const post = (apiUrl: string, params?: any): Promise<AxiosResponse> => {
  let postData = {}
  if (params && params.pageNo && params.pageSize) {
    const data = cloneDeep(params)

    const pager = {
      pageNo: data.pageNo,
      pageSize: data.pageSize
    }

    const pagerParams = { pager: pager }

    const remainderParams = omit(data, ['pageNo', 'pageSize', 'options'])

    postData = assign(pagerParams, { options: data.options }, remainderParams)
  } else {
    postData = params
  }
  return axios.post(apiUrl, postData)
}

// export funciton get(apiUrl:string, params?:any):Promise<AxiosResponse>{

// }
